import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { retry } from 'rxjs/operators';
import { DomSanitizer } from '@angular/platform-browser';

declare var form: any;
declare var base: any;
declare var security: any;

@Component({
  templateUrl: './reg.service.html'
})
export class RegServiceComponent implements OnInit {

  fs: any = null;
  serTypeDic: any;
  sysDic: any;
  ffs: any = null;

  interfaceVisible = false;
  ifRow: any = {};
  updateToken = 'false';

  constructor(private http: HttpClient, private domSanitizer: DomSanitizer) {
    this.fs = new form.FormSimpleService('registerservice', {
      orderObject: { order: 0 },
      keyField: 'id'
    });
    this.fs.loadData(true);

    // 初始化字典
    this.serTypeDic = new form.DictionaryService('p_service_type');
    this.sysDic = new form.DictionaryService('p_sys_service');
    this.sysDic.loadAll();

    // 文件服务
    this.ffs = new form.FileService();
  }

  ngOnInit() {
  }

  editSSO(row) {
    const fo = this.fs;
    if (fo.isLoading) {
      return false;
    }

    fo.isLoading = true;
    const id = row.id;
    const jwts = security.SecurityService.jwt;
    const url = base.BaseService.getHostUrl() + '/registerservice/regSSO/' + id;
    const obsData = this.http.post<any>(url, '', { headers: new HttpHeaders({ jwt: jwts }) })
      .pipe(retry(1));
    obsData.subscribe(
      (data) => {
        fo.isLoading = false;
        row.ssoid = data.data;
      },
      () => {
        fo.isLoading = false;
      }
    );
  }

  editInterface(row) {
    const fo = this.fs;
    fo.isLoading = true;
    this.ifRow = row;
    const tokenid = row.id;
    const tokenpwd = row.ifprovide;
    const jwts = security.SecurityService.jwt;
    const url = base.BaseService.getHostUrl() + '/token/getTokens/' + tokenid + '/' + tokenpwd;
    const obsData = this.http.post<any>(url, this.ifRow.ifpower, { headers: new HttpHeaders({ jwt: jwts }) })
      .pipe(retry(1));
    obsData.subscribe(
      (data) => {
        if (data.code === '-1' && data.data) {
          this.ifRow.ifpower = data.data;
        } else {
          this.ifRow.ifpower = [];
        }
        fo.isLoading = false;
        this.interfaceVisible = true;
      },
      () => {
        this.ifRow.ifpower = [];
        fo.isLoading = false;
      }
    );
  }

  saveInterface() {
    const fo = this.fs;
    if (fo.isSaveing) {
      return false;
    }

    fo.isSaveing = true;
    const id = this.ifRow.id;
    const jwts = security.SecurityService.jwt;
    const url = base.BaseService.getHostUrl() + '/registerservice/regToken/' + id + '/' + this.updateToken;
    const obsData = this.http.post<any>(url, this.ifRow.ifpower, { headers: new HttpHeaders({ jwt: jwts }) })
      .pipe(retry(1));
    obsData.subscribe(
      (data) => {
        this.ifRow.ifprovide = data.data.ifprovide;
        this.interfaceVisible = false;
        fo.isSaveing = false;
      },
      () => {
        this.interfaceVisible = false;
        fo.isSaveing = false;
      }
    );
  }

  closeInterface() {
    this.interfaceVisible = false;
  }

  checked(e, s: any[]) {
    const id = e.node.key;
    if (e.node.isChecked) {
      if (s.indexOf(id) === -1) {
        s.push(id);
      }
    } else {
      if (s.concat(id)) {
        s.splice(s.indexOf(id), 1);
      }
    }
  }
}
